package db;

import java.sql.SQLException;

import core.common.Configurations;
import core.pool.iResourceFactory;

public class DBConnectionFactory
        implements iResourceFactory<DBConnection> {

    private static String connectionURL;
    private static String userName;
    private static String password;

    public DBConnectionFactory() {
        super();

        try {
            // this will load the MySQL JDBC driver
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException ce) {
            throw new IllegalArgumentException(
                    "Unable to find driver in classpath", ce);
        }

        connectionURL = Configurations.getConnectionUrl();
        userName = Configurations.getDbUser();
        password = Configurations.getDbPass();
    }

    @Override
    public DBConnection create() {
        try {
            return new DBConnection(connectionURL, userName, password);
        } catch (SQLException | ClassNotFoundException e) {
            e.printStackTrace();
            return null;
        }
    }
}
